﻿using KPIS.GERP.ESS.APP.App_UI.Commons;
using KPIS.GERP.ESS.DAL;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace KPIS.GERP.ESS.APP.App_UI.Forms
{
    public partial class ESS_ProjectOperationEditSummary : System.Web.UI.Page
    {
        Main main = new Main();
        BGM_Project.BGM_Project pj = new BGM_Project.BGM_Project();
        BGM_ProjectOperation.BGM_ProjectOperation po = new BGM_ProjectOperation.BGM_ProjectOperation();
        BGM_ProjectOperationPerson.BGM_ProjectOperationPerson poper = new BGM_ProjectOperationPerson.BGM_ProjectOperationPerson();
        BGM_MAS_Option.BGM_MAS_Option op = new BGM_MAS_Option.BGM_MAS_Option();
        D_MessageSend send = new D_MessageSend();
        D_MessageReply reply = new D_MessageReply();
        D_BgmProjectOperationEdit edit = new D_BgmProjectOperationEdit();

        protected void Page_Load(object sender, EventArgs e)
        {
            lblBgmProjOperEditSeq.Text = Session["ReportSeq"].ToString();
            LoadData();
        }


        protected void LoadData()
        {
            string strBgmProjOperEditSeq = lblBgmProjOperEditSeq.Text;
            var editInfo = edit.GetInfo(Convert.ToInt32(strBgmProjOperEditSeq));


            BGM_ProjectOperation.M_BGM_ProjectOperation operInfo = new BGM_ProjectOperation.M_BGM_ProjectOperation();
            operInfo = po.GetInfo(editInfo.bgm_proj_oper_seq.ToString());


            BGM_Project.M_BGM_Project projInfo = new BGM_Project.M_BGM_Project();
            projInfo = pj.GetInfo(operInfo.bgm_proj_seq.ToString());

            

            lblFinBgmSubActName.Text = main.FinString(projInfo.m_bgm_subactivity.bgm_strat_list_name);
            lblFinBgmActName.Text = main.FinString(projInfo.m_bgm_activity.bgm_strat_list_name);
            lblFinBgmOutputName.Text = main.FinString(projInfo.m_bgm_output.bgm_strat_list_name);
            lblFinBgmProjName.Text = main.FinString(projInfo.bgm_proj_name);

            lblFinAttendCount.Text = main.FinString((operInfo.attend_count ?? 0).ToString("#,##0"));
            lblFinBgmProjOperDate.Text = operInfo.bgm_proj_oper_date.HasValue ? operInfo.bgm_proj_oper_date.Value.ToLongDateString() : "-";
            lblFinBgmProjOperName.Text = main.FinString(operInfo.bgm_proj_oper_name);
            lblFinBgmProjOperCode.Text = main.FinString(editInfo.bgm_proj_oper_edit_code);
            lblFinBgmYear.Text = main.FinString(operInfo.bgm_year.ToString());
            lblFinEndDate.Text = operInfo.start_date.HasValue ? operInfo.start_date.Value.ToLongDateString() : "-";
            lblFinStartDate.Text = operInfo.end_date.HasValue ? operInfo.end_date.Value.ToLongDateString() : "-";
            lblFinRecordStatus.Text = main.FinString(editInfo.record_status);
            lblSendBackRemark.Text = "";
            tableSendBackRemark.Visible = false;

            switch (lblFinRecordStatus.Text)
            {
                case "W":
                    lblFinRecordStatusName.Text = "ดำเนินการอยู่";
                    lblFinRecordStatusName.ForeColor = System.Drawing.Color.Red;
                    break;
                case "P":
                    lblFinRecordStatusName.Text = "รอการอนุมัติโดย " + send.GetLatestMessageStatus(Convert.ToInt32(strBgmProjOperEditSeq), "bgm_project_operation_edit").to_user_name;
                    lblFinRecordStatusName.ForeColor = System.Drawing.Color.Blue;
                    break;
                case "A":
                    lblFinRecordStatusName.Text = "อนุมัติโดย " + send.GetLatestMessageStatus(Convert.ToInt32(strBgmProjOperEditSeq), "bgm_project_operation_edit").to_user_name;
                    lblFinRecordStatusName.ForeColor = System.Drawing.Color.Green;
                    break;
                case "R":
                    lblFinRecordStatusName.Text = "ตีกลับ " + send.GetLatestMessageStatus(Convert.ToInt32(strBgmProjOperEditSeq), "bgm_project_operation_edit").to_user_name;
                    lblFinRecordStatusName.ForeColor = System.Drawing.Color.Red;
                    tableSendBackRemark.Visible = true;

                    var replys = reply.GetInfoFromRefSeq(Convert.ToInt32(strBgmProjOperEditSeq), "bgm_project_operation_edit");

                    if (replys.Count > 0)
                    {
                        lblSendBackRemark.Text = replys[0].remarks;
                    }

                    //lblSendBackRemark.Text = reply.GetInfoFromRefSeq(strBgmProjOperSeq, "bgm_project_operation")[0].remarks;
                    break;
                default:
                    lblFinRecordStatusName.Text = "รอการยืนยัน " + send.GetLatestMessageStatus(Convert.ToInt32(strBgmProjOperEditSeq), "bgm_project_operation_edit").to_user_name;
                    lblFinRecordStatusName.ForeColor = System.Drawing.Color.Red;
                    break;
            }

            LoadFinProjectExpense(strBgmProjOperEditSeq);

            IList<BGM_MAS_Option.M_BGM_MAS_Option> optionInfoList = op.GetListFromBgmProjSeq("3", operInfo.bgm_proj_seq.ToString());// HardCode
            Repeater2.DataSource = optionInfoList;
            Repeater2.DataBind();

            IList<BGM_ProjectOperationPerson.M_BGM_ProjectOperationPerson> personInfoList = poper.GetFull(editInfo.bgm_proj_oper_seq.ToString());
            Repeater3.DataSource = personInfoList;
            Repeater3.DataBind();
        }

        protected void LoadFinProjectExpense(string strBgmProjOperEditSeq)
        {
            var masterListInfo = edit.GetExpenseFull(strBgmProjOperEditSeq);
            
            RepeaterBgmSource.DataSource = masterListInfo
                .GroupBy(m => new { m.bgm_source_seq, m.bgm_source_name })
                .Select(m => new { bgm_source_seq = m.Key.bgm_source_seq, bgm_source_name = m.Key.bgm_source_name, add_amount = m.Sum(s => s.add_amount) });
            RepeaterBgmSource.DataBind();

            foreach (RepeaterItem item in RepeaterBgmSource.Items)
            {
                Repeater RepeaterBgmExp = (Repeater)item.FindControl("RepeaterBgmExp");
                Label lblBgmSourceSeq = (Label)item.FindControl("lblBgmSourceSeq");

                RepeaterBgmExp.DataSource = masterListInfo.Where(m => m.bgm_source_seq == main.ConvertNullInt(lblBgmSourceSeq.Text));
                RepeaterBgmExp.DataBind();
            }

            lblFinSumAmount.Text = masterListInfo.Sum(m => m.add_amount).ToString("#,##0.00");
        }

        protected void cmdExportToMsWord_Click(object sender, EventArgs e)
        {
            ExportToMsWord();
        }

        protected void ExportToMsWord()
        {
            string strFileName = "Export-.doc";
            Response.Clear();
            Response.Buffer = true;
            Response.ContentType = "application/vnd.ms-word";
            Response.ContentEncoding = System.Text.Encoding.UTF8;
            Response.AddHeader("Content-Disposition", "attachment;filename=" + strFileName);
            //EnableViewState = false;
            System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
            System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
            divSummary.RenderControl(oHtmlTextWriter);
            Response.Output.Write(oStringWriter.ToString());
            Response.Flush();
            Response.End();
        }
    }
}